package net.ecpt.crm.services;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.poi.EncryptedDocumentException;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.ServiceUtil;
import org.ofbiz.wvtools.utils.ExcelPoiUtils;

import javolution.util.FastList;
import javolution.util.FastMap;

public class CrmInventoryXlsServices {
    public static final String module = CrmInventoryXlsServices.class.getName();

    /**
     * 导入
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> importLiveupdateInventoryXls(DispatchContext dctx, Map<String, ? extends Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Delegator delegator = dctx.getDelegator();
	GenericValue userLogin = (GenericValue)context.get("userLogin");
	List<String> messageList = new ArrayList<String>();
	List<Map<String, Object>> sheetList = (List<Map<String, Object>>) context.get("sheetList");
	try {
	    int i = 0;
	    List<GenericValue> entityList = FastList.newInstance();
	    for (Map<String, Object> row : sheetList) {
		i = i + 1;
		String warehouseCode = ExcelPoiUtils.getCellString(row, 0);//仓库编码
		String physicalFreeze = ExcelPoiUtils.getCellString(row, 1);//已使用冷冻
		String physicalRefrigerate = ExcelPoiUtils.getCellString(row, 2);//已使用冷藏
		String physicalConstant = ExcelPoiUtils.getCellString(row, 3);//已使用恒温
		String physicalNormal = ExcelPoiUtils.getCellString(row, 4);//已使用常温
		

	
		if (UtilValidate.isEmpty(warehouseCode)) {
		    messageList.add("第" + i + "行,仓库编码不能为空。");
		    continue;
		}
		
		
		List<GenericValue> crmWmsInventoryInfo = delegator.findByAnd("CrmWmsInventory",UtilMisc.toMap("warehouseCode", warehouseCode),UtilMisc.toList("id"),false);
		
		GenericValue crmWmsInventory =EntityUtil.getFirst(crmWmsInventoryInfo);
		crmWmsInventory.set("physicalFreeze", physicalFreeze);
		crmWmsInventory.set("physicalRefrigerate", physicalRefrigerate);
		crmWmsInventory.set("physicalConstant", physicalConstant);
		crmWmsInventory.set("physicalNormal", physicalNormal);
		
		crmWmsInventory.set("updateTime", UtilDateTime.nowTimestamp());

		crmWmsInventory.set("ownerUserId", userLogin.getString("partyId"));
		//crmWmsInventory.set("statusId", "0");
		entityList.add(crmWmsInventory);
		
		entityList.add(createCrmInventoryHistory(delegator,crmWmsInventory));
//		company.create();

	    }
	    delegator.storeAll(entityList);
	    result.put("messageList", messageList);
	} catch (EncryptedDocumentException e) {
		Debug.logError(e.getMessage(), module);
	} catch (GenericEntityException e) {
		Debug.logError(e.getMessage(), module);
	}
	return result;
    }
    
    
    private static GenericValue createCrmInventoryHistory(Delegator delegator,GenericValue crmWmsInventory){
    	String id = GenerateIdUtils.getId();
		
		
		GenericValue entity = delegator.makeValue("CrmInventoryHistory",UtilMisc.toMap("id", id));
		
		entity.set("warehouseId", crmWmsInventory.get("warehouseId"));
		
		entity.set("physicalFreeze", crmWmsInventory.get("physicalFreeze"));
		entity.set("physicalRefrigerate", crmWmsInventory.get("physicalRefrigerate"));
		entity.set("physicalConstant", crmWmsInventory.get("physicalConstant"));
		entity.set("physicalNormal", crmWmsInventory.get("physicalNormal"));
		
		entity.set("createTime", crmWmsInventory.get("updateTime"));

		entity.set("createUserId", crmWmsInventory.get("ownerUserId"));
		return entity;

    }
    
    
    
    /**
     * 导出模板
     */

    public static Map<String, Object> exportLiveupdateInventoryXls(DispatchContext dctx, Map<String, ? extends Object> context) {
	Delegator delegator = dctx.getDelegator();

	Map<String, Object> result = ServiceUtil.returnSuccess();

	
	try {
	    List<GenericValue> entityList = delegator.findAll("CrmWmsInventory", false);

	    List<Map<String, Object>> list = FastList.newInstance();
	    Map<String, Object> keyMap = FastMap.newInstance();
	    keyMap.put("0", "warehouseCode");
	    keyMap.put("1", "physicalFreeze");
	    keyMap.put("2", "physicalRefrigerate");
	    keyMap.put("3", "physicalConstant");
	    keyMap.put("4", "physicalNormal");
	    
	    String headers = "仓库编码,已使用冷冻,已使用冷藏,已使用恒温,已使用常温";
	    result.put("headers", headers);
	    
	    
	    

	    for (GenericValue entity : entityList) {
		Map<String, Object> map = FastMap.newInstance();
		String warehouseCode = entity.getString("warehouseCode");
		BigDecimal physicalFreeze = entity.getBigDecimal("physicalFreeze");
		BigDecimal physicalRefrigerate = entity.getBigDecimal("physicalRefrigerate");
		BigDecimal physicalConstant = entity.getBigDecimal("physicalConstant");
		BigDecimal physicalNormal = entity.getBigDecimal("physicalNormal");
		
		
		
	    map.put("warehouseCode", warehouseCode);
		map.put("physicalFreeze", physicalFreeze);
		map.put("physicalRefrigerate", physicalRefrigerate);
		map.put("physicalConstant", physicalConstant);
		map.put("physicalNormal", physicalNormal);
		
		list.add(map);
	    }
	    result.put("list", list);
	    result.put("keyMap", keyMap);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}
	return result;
    }

    

    
}
